﻿using BusinessEntity;
using BusinessRepository;
using learun.util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule
{
    public class BaseEditPower
    {


        public static string GetSPEnumValue(string enumName = "待提交")
        {
            try
            {
                Type enumType = typeof(BaseEditPower.SPStatus);
                if (!enumType.IsEnum)
                    throw new ArgumentException("enumType必须是枚举类型");
                var values = Enum.GetValues(enumType);
                var ht = new Hashtable();
                foreach (var val in values)
                {
                    ht.Add(Enum.GetName(enumType, val), val);
                }
                return ((int)ht[enumName]).ToString();
                ;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static int GetEnumValue(Type enumType, string enumName)
        {
            try
            {
                if (!enumType.IsEnum)
                    throw new ArgumentException("enumType必须是枚举类型");
                var values = Enum.GetValues(enumType);
                var ht = new Hashtable();
                foreach (var val in values)
                {
                    ht.Add(Enum.GetName(enumType, val), val);
                }
                return (int)ht[enumName];
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public enum ModuleName
        {
            采购合同 = 0,//     
            项目合同 = 1,
            通用付款合同 = 2,
            租赁合同 = 3,
            通用收款合同 = 4
        }

        public enum JSStatus
        {
            待填报 = 0,
            项目送审 = 1,
            项目审计 = 2
        }

        public enum ZBStatus
        {
            待填报 = 0,
            填报申请 = 1,
            确认标底 = 2
        }

        public enum SPStatus
        {
            待提交 = -1,
            待审核 = 0,
            审核中 = 1,
            通过 = 2,
            已驳回 = 3,
            已作废 = 4
        }

        public enum JumpModule
        {
            投资计划,//y  已关联
            概算,//y
            采购计划,//y 已关联
            竣工验收计划,//y
            月付款计划,//y 已关联
            合同金额限制,//y 已关联
            申请金额限制

        }

        public enum TableNameBM
        {
            投资计划,
            项目前期,
        }

        public enum TableName
        {
            投资计划,
            投资计划结转,
            投资计划变更,
            项目,
            项目前期,//不审批
            项目计划开工,
            项目预算,
            项目预算变更,

            概算,
            概算明细,
            概算变更,

            单项,

            招标代理机构,
            采购计划,
            编标计划,
            招标,
            合同,

            合同修改记录,

            合同保证金,

            设计变更,
            合同变更,
            变更协议,
            结算,
            竣工,
            合同缺陷期,



            付款申请,
            付款月计划,
            日志,
            日志report,
            日志safe,

            用章,
            归档,
            决算,
            移交,
            历史库
        }

        //审核中修改审定金额
        public static Dictionary<string, string> listSHJE = new Dictionary<string, string>()
        {
            ["投资计划结转"] = "bnjhapje",
            ["付款月计划"] = "shje"
        };

        //审核回调 修改数据状态
        public static Dictionary<string, string> listTableName = new Dictionary<string, string>()
        {
            ["投资计划"] = "CProTZJH",
            ["投资计划结转"] = "CProTZJHND",
            ["投资计划变更"] = "CProTZJHbgS",

            ["合同保证金"] = "CProConBZJ",
            ["合同修改记录"] = "CProContractbglog",



            ["项目"] = "CProManage",
            ["项目前期"] = "CProXMQQ",
            ["项目计划开工"] = "CProManageJHKG",
            ["项目预算"] = "CProManCZBK",
            ["项目预算变更"] = "CProManCZBKbg",



            ["概算"] = "CProManageGSpc",
            ["概算明细"] = "CProManageGS",

            ["概算变更"] = "CProManageGSbgS",

            ["招标代理机构"] = "CProZBdljg",

            ["采购计划"] = "CProZBLS",
            ["编标计划"] = "CProZBBianB",

            ["单项"] = "CProManageLX",
            ["招标"] = "CProZB",
            ["合同"] = "CProContract",
            ["结算"] = "CProJS",
            ["竣工"] = "CProJG",
            ["合同缺陷期"] = "CProConQXQ",


            ["付款申请"] = "CProFKSQ",
            ["设计变更"] = "CProConBGSJ",
            ["合同变更"] = "CProConBG",
            ["变更协议"] = "CProConBGxy",



            ["付款月计划"] = "CProFKYJH",
            ["日志"] = "CProConLog",
            ["用章"] = "CProYongZhang",
            ["归档"] = "CProGD",
            ["决算"] = "CProJUS",
            ["移交"] = "CProYiJiao",
            ["历史库"] = "CProJUS",
        };

        //这里是和 编码的type 做对应
        //public static Dictionary<string, string> listTableName = new Dictionary<string, string>()
        //{
        //    ["投资计划"] = "CProTZJH",
        //    ["投资计划结转"] = "CProTZJHND",
        //    ["投资计划变更"] = "CProTZJHbgS",

        //    ["项目"] = "CProManage",
        //    ["概算"] = "CProManageGSpc",

        //    ["概算变更"] = "CProManageGSbgS",
        //    ["采购计划"] = "CProZBLS",
        //    ["编标计划"] = "CProZBBianB",

        //    ["单项"] = "CProManageLX",
        //    ["招标"] = "CProZB",
        //    ["合同"] = "CProContract",
        //    ["结算"] = "CProJS",
        //    ["竣工"] = "CProJG",
        //    ["付款申请"] = "CProFKSQ",
        //    ["设计变更"] = "CProConBGSJ",
        //    ["合同变更"] = "CProConBG",
        //    ["变更协议"] = "CProConBGxy",



        //    ["付款月计划"] = "CProFKYJH",
        //    ["日志"] = "CProConLog",
        //    ["用章"] = "CProYongZhang",
        //    ["归档"] = "CProGD",
        //    ["决算"] = "CProJUS",
        //    ["移交"] = "CProYiJiao",
        //    ["历史库"] = "CProJUS",
        //};

        #region 首页详细信息
        public static Dictionary<string, string> SPDetailUrl = new Dictionary<string, string>()
        {
            //            const Routes = [
            //  '审核投资计划结转 => /project/plan/investment/:id/transfer/:planYearId/audit?taskId=:taskId',
            //  '审核投资计划变更 => /project/plan/investment/:id?/plan-year/:planYearId/modify/:modifyId/audit?taskId=:taskId',
            //  '审核项目概算变更 => /project/setup/estimate/:estimateId/modify/:modifyId/audit?taskId=:taskId',
            //  '概算批次审核 => /project/setup/main/:id/estimate-batch/:estimateBatchId/audit?taskId=:taskId',
            //  '变更申请审核 => /project/change/:id/:taskId/audit',

            //  '审核项目竣工验收 => /project/audit/completed-acceptance-audit/:type/:taskId/:jgId',

            //  '审核项目送审 => /project/audit/project-approval-audit/:type/:category/:taskId/:id',
            //  '项目采购审核 => /project/procurement-bidding/procurement/:id/:taskId/audit',
            //  '审核招标信息 => /project/procurement-bidding/bidding/:dxid/:taskId/invite/audit',
            //  '审核项目合同 => /project/procurement-bidding/contract/:id/:taskId/audit',
            //  '审核项目补充协议 => /project/procurement-bidding/supple/:id/:cid/:taskId/audit',
            //  '审核月付款计划 => /project/funds/monthly-payment-audit/:type/:taskId/:id',

            //  '审核付款申请 => /project/funds/payment-application-audit/:type/:sfgl/:taskId/:id/:htid',
            //  '审核用章管理  => /project/agreement/seal-audit/:type?taskId=:taskId'
            //]


            ["项目预算变更"] = "/project/capital/change/${taskId}/:id/audit",

            ["项目计划开工"] = "/project/setup/start/:id/${taskId}/audit",
            ["合同修改记录"] = "/project/procurement-bidding/contract/${taskId}/:id/change-audit/audit",

            ["项目预算"] = "/project/capital/capital/${taskId}/:id/audit",


            ["合同保证金"] = "/project/procurement-bidding/letter/${taskId}/:htid/:id/audit",
            ["单项"] = "/project/setup/main/:projectId/tender-section/:id/audit?taskId=${taskId}",

            ["合同缺陷期"] = "/defect-removal-audit/audit/${taskId}/:id",


            ["编标计划"] = "/project/procurement-bidding/compile/:dxid/:id/${taskId}/audit",
            ["设计变更"] = "/project/change/scheme-change/:id/${taskId}/audit",
            ["招标代理机构"] = "/project/procurement-bidding/agency-apply/:id/${taskId}/audit",

            //"/project/plan/investment/:id/info/audit?taskId=${taskId}"
            ["投资计划"] = "/project/plan/investment/:id/audit?taskId=${taskId}",
            //"/project/plan/investment/:id/transfer/:planYearId/audit?taskId=${taskId}"
            ///project/plan/investment/:id/transfer/:planYearId/view
            ///project/plan/investment/:id/transfer/:planYearId/audit
            ["投资计划结转"] = "/project/plan/investment/:id/transfer/:planYearId/audit?taskId=${taskId}",
            //"/project/plan/investment/plan-year/:planYearId/modify/:modifyId/audit?taskId=${taskId}"
            ["投资计划变更"] = "/project/plan/investment/:id/plan-year/:planYearId/modify/:modifyId/audit?taskId=${taskId}",
            ///project/setup/main/:id/info/audit?taskId=${taskId}&formCode=project_setup_attach
            ////project/setup/main/:id/estimate/audit?taskId=${taskId}&formCode=project_setup_attach
            ["项目"] = "/project/setup/main/:id/info/audit?taskId=${taskId}&formCode=project_setup_attach",

            //["项目计划开工"] = "/project/setup/main/:id/info/audit?taskId=${taskId}&formCode=project_setup_attach",

            //"/project/setup/main/:id/estimate-batch/:estimateBatchId/audit?taskId=${taskId}"
            ["概算"] = "/project/setup/:id/estimate/batch/:estimateBatchId/audit?taskId=${taskId}",
            //"/project/setup/estimate/:estimateId/modify/:modifyId/confirm?taskId=${taskId}"
            ["概算变更"] = "/project/setup/estimate/:estimateId/modify/:modifyId/audit?taskId=${taskId}",

            //["单项"] = "CProManageLX",
            ///project/procurement-bidding/bidding/:dxid/win/view
            ["采购计划"] = "/project/procurement-bidding/procurement/:id/${taskId}/audit",
            ["招标"] = "/project/procurement-bidding/bidding/:dxid/${taskId}/invite/audit",
            ["合同"] = "/project/procurement-bidding/contract/:id/${taskId}/audit",
            ///project/procurement-bidding/contract/${taskId}/${id}/change-audit/audit
            ////project/change/:id/${taskId}/audit
            ///"/project/procurement-bidding/contract/${taskId}/:id/change-audit/audit",
            ////project/procurement-bidding/contract/${id}/change-view/view
            ["合同变更"] = "/project/change/:id/${taskId}/audit",
            ["变更协议"] = "/project/procurement-bidding/supple/:id/:cid/${taskId}/audit",

            ["结算"] = "/project/audit/project-approval-audit/audit/projectReview/${taskId}/:id",
            ["竣工"] = "project/audit/completed-acceptance-audit/audit/${taskId}/:jgId",
            ["付款月计划"] = "/project/funds/monthly-payment-audit/audit/${taskId}/:id",
            ["付款申请"] = "/project/funds/payment-application-audit/audit/:sfgl/${taskId}/:id/:htid",


            ["日志"] = "CProConLog",
            ["用章"] = "/project/agreement/seal-audit/audit/${taskId}/:id",
            ["归档"] = "CProGD",
            ["决算"] = "CProJUS",
            ["移交"] = "CProYiJiao",
            ["历史库"] = "CProJUS",
        };

        public static Dictionary<string, string> ViewDetailUrl = new Dictionary<string, string>()
        {
            //            const Routes = [
            //  '审核投资计划结转 => /project/plan/investment/:id/transfer/:planYearId/audit?taskId=:taskId',
            //  '审核投资计划变更 => /project/plan/investment/:id?/plan-year/:planYearId/modify/:modifyId/audit?taskId=:taskId',
            //  '审核项目概算变更 => /project/setup/estimate/:estimateId/modify/:modifyId/audit?taskId=:taskId',
            //  '概算批次审核 => /project/setup/main/:id/estimate-batch/:estimateBatchId/audit?taskId=:taskId',
            //  '变更申请审核 => /project/change/:id/:taskId/audit',

            //  '审核项目竣工验收 => /project/audit/completed-acceptance-audit/:type/:taskId/:jgId',

            //  '审核项目送审 => /project/audit/project-approval-audit/:type/:category/:taskId/:id',
            //  '项目采购审核 => /project/procurement-bidding/procurement/:id/:taskId/audit',
            //  '审核招标信息 => /project/procurement-bidding/bidding/:dxid/:taskId/invite/audit',
            //  '审核项目合同 => /project/procurement-bidding/contract/:id/:taskId/audit',
            //  '审核项目补充协议 => /project/procurement-bidding/supple/:id/:cid/:taskId/audit',
            //  '审核月付款计划 => /project/funds/monthly-payment-audit/:type/:taskId/:id',

            //  '审核付款申请 => /project/funds/payment-application-audit/:type/:sfgl/:taskId/:id/:htid',
            //  '审核用章管理  => /project/agreement/seal-audit/:type?taskId=:taskId'
            //]

            ["项目预算变更"] = "/project/capital/change/:id/view",

            ["项目计划开工"] = "project/setup/start/:id/update",
            ["合同修改记录"] = "/project/procurement-bidding/contract/:id/change-view/view",
            ["项目预算"] = "/project/capital/capital/:id/view",


            ["合同保证金"] = "/project/procurement-bidding/letter/:htid/:id/view",
            ["单项"] = "/project/setup/main/:projectId/tender-section/:id/edit",

            ["合同缺陷期"] = "/defect-removal-details/see/:id",

            ["编标计划"] = "/project/procurement-bidding/compile/:dxid/:id/view",
            ["设计变更"] = "/project/change/scheme-change/:id/see",
            ["招标代理机构"] = "/project/procurement-bidding/agency-apply/:id/view",

            // "/project/plan/investment/:id/info/view",
            ["投资计划"] = "/project/plan/investment/:id/view",
            //"project/plan/investment/:id?/plan-year/:planYearId/modify/:modifyId/view"
            ["投资计划结转"] = "/project/plan/investment/:id/transfer/:planYearId/view",
            //"/project/plan/investment/:id/transfer/:planYearId/view",
            ["投资计划变更"] = "/project/plan/investment/:id/plan-year/:planYearId/modify/:modifyId/view",
            //"/project/setup/main/:id/info/view?formCode=project_setup_attach",
            ///project/setup/main/:id/info/view
            ///project/setup/main/:id/estimate/info?formCode=project_setup_attach
            ["项目"] = "/project/setup/main/:id/info/view",

            //["项目计划开工"] = "/project/setup/main/:id/info/audit?taskId=${taskId}&formCode=project_setup_attach",

            //"/project/setup/main/:id/estimate/info"
            ["概算"] = "/project/setup/estimate/batch/:id/info",
            //"/project/setup/estimate/:estimateId/modify/:modifyId/view"
            ["概算变更"] = "/project/setup/estimate/:estimateId/modify/:modifyId/view",

            //["单项"] = "CProManageLX",
            ///project/procurement-bidding/bidding/:dxid/win/view
            ["采购计划"] = "/project/procurement-bidding/procurement/:id/view",



            ["招标"] = "/project/procurement-bidding/bidding/:dxid/invite/view",
            ["合同"] = "/project/procurement-bidding/contract/:id/view",

            ///project/change/:id/view "/project/procurement-bidding/contract/:id/change-view/view",
            ["合同变更"] = "/project/change/:id/view",

            ["变更协议"] = "/project/procurement-bidding/supple/:id/:cid/view",

            ["结算"] = "/project/audit/project-approval-details/view/projectReview/:id",
            ["竣工"] = "project/audit/completed-acceptance-details/view/:jgId",
            ["付款月计划"] = "/project/funds/monthly-payment-details/view/:id/:htid",
            ["付款申请"] = "/project/funds/payment-application-details/view/:id/:htid/:sfgl",


            ["日志"] = "CProConLog",
            ["用章"] = "/project/agreement/seal-audit/view/:id",
            ["归档"] = "CProGD",
            ["决算"] = "CProJUS",
            ["移交"] = "CProYiJiao",
            ["历史库"] = "CProJUS",
        };

        public static (string, string) GetSPDetailUrlS(string strBname, params string[] strargs)
        {
            string auditurl = GetSPDetailUrl(strBname, strargs);
            string viewurl = GetViewDetailUrl(strBname, strargs);

            return (auditurl, viewurl);
        }

        //public static (string, string) GetSPDetailUrlSBy(string strBname, params string[] strargs)
        //{
        //    string auditurl = GetSPDetailUrl(strBname, strargs);
        //    string viewurl = GetViewDetailUrl(strBname, strargs);

        //    return (auditurl, viewurl);
        //}

        public static string GetSPDetailUrl(string strBname, params string[] strargs)
        {
            string strurl = SPDetailUrl[strBname];

            if (strBname == "投资计划")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "项目预算")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "项目预算变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "项目计划开工")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "单项")
            {
                strurl = strurl.Replace(":projectId", strargs[0]);
                strurl = strurl.Replace(":id", strargs[1]);
            }
            else if (strBname == "合同保证金")
            {
                strurl = strurl.Replace(":htid", strargs[0]);
                strurl = strurl.Replace(":id", strargs[1]);
            }
            else if (strBname == "合同缺陷期")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }

            else if (strBname == "编标计划")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":dxid", strargs[1]);
            }
            else if (strBname == "设计变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "招标代理机构")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }



            else if (strBname == "投资计划结转")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":planYearId", strargs[1]);
            }
            else if (strBname == "投资计划变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":planYearId", strargs[1]);
                strurl = strurl.Replace(":modifyId", strargs[2]);

            }
            else if (strBname == "项目")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "概算")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":estimateBatchId", strargs[1]);

            }
            else if (strBname == "概算变更")
            {
                strurl = strurl.Replace(":estimateId", strargs[0]);
                strurl = strurl.Replace(":modifyId", strargs[1]);
            }


            else if (strBname == "采购计划")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "招标")
            {
                strurl = strurl.Replace(":dxid", strargs[0]);
            }
            else if (strBname == "合同")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "合同修改记录")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            
            else if (strBname == "合同变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "变更协议")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":cid", strargs[1]);
            }


            else if (strBname == "结算")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "竣工")
            {
                strurl = strurl.Replace(":jgId", strargs[0]);
            }
            else if (strBname == "付款月计划")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                //这里多一个
                strurl = strurl.Replace(":htid", strargs[1]);
            }
            else if (strBname == "付款申请")
            {
                strurl = strurl.Replace(":sfgl", strargs[0]);
                strurl = strurl.Replace(":id", strargs[1]);
                strurl = strurl.Replace(":htid", strargs[2]);
            }

            return strurl;
        }

        public static string GetViewDetailUrl(string strBname, params string[] strargs)
        {
            string strurl = ViewDetailUrl[strBname];

            if (strBname == "投资计划")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "项目预算")
            {
                strurl = strurl.Replace(":id", strargs[0]);


            }
            else if (strBname == "项目预算变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);

            }

            else if (strBname == "项目计划开工")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "单项")
            {
                strurl = strurl.Replace(":projectId", strargs[0]);
                strurl = strurl.Replace(":id", strargs[1]);
            }
            else if (strBname == "合同保证金")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "合同缺陷期")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "编标计划")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":dxid", strargs[1]);
            }
            else if (strBname == "设计变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "招标代理机构")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }

            else if (strBname == "投资计划结转")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":planYearId", strargs[1]);
            }
            else if (strBname == "投资计划变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":planYearId", strargs[1]);
                strurl = strurl.Replace(":modifyId", strargs[2]);
            }
            else if (strBname == "项目")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "概算")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":estimateBatchId", strargs[1]);

            }
            else if (strBname == "概算变更")
            {
                strurl = strurl.Replace(":estimateId", strargs[0]);
                strurl = strurl.Replace(":modifyId", strargs[1]);
            }


            else if (strBname == "采购计划")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "招标")
            {
                strurl = strurl.Replace(":dxid", strargs[0]);
            }
            else if (strBname == "合同")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "合同修改记录")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "设计变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "合同变更")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "变更协议")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                strurl = strurl.Replace(":cid", strargs[1]);
            }


            else if (strBname == "结算")
            {
                strurl = strurl.Replace(":id", strargs[0]);
            }
            else if (strBname == "竣工")
            {
                strurl = strurl.Replace(":jgId", strargs[0]);
            }
            else if (strBname == "付款月计划")
            {
                strurl = strurl.Replace(":id", strargs[0]);
                //这里多一个
                strurl = strurl.Replace(":htid", strargs[1]);
            }
            else if (strBname == "付款申请")
            {
                strurl = strurl.Replace(":sfgl", strargs[0]);
                strurl = strurl.Replace(":id", strargs[1]);
                strurl = strurl.Replace(":htid", strargs[2]);
            }

            return strurl;
        }

        #endregion

        public static async Task<bool> GetEditPowerOpe(string ywid, string name, string strope)
        {
            //strope = Enum.GetName( CommonHelper.ParseInt(strope));

            string strsql = "select status  from " + listTableName[name] + " where id = '" + ywid + "'";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(strsql, null);
            bool cancz = false;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                string strstatus = table.Rows[i]["status"].ToString();

                //(case when ISNULL(status,-1) = '-1' then '待提交'
                //when ISNULL(status,-1) = '0' then '待审批'
                //when ISNULL(status,-1) = '1' then '审批中'
                //when ISNULL(status,-1) = '2' then '审核通过'
                //when ISNULL(status,-1)  = '3' then '已驳回'
                //when ISNULL(status,-1)  = '4' then '已作废'
                //end


                if (strope == "0")//待审批
                {
                    if (strstatus == "-1")//待提交
                    {
                        cancz = true;
                    }
                }

                if (strope == "1")//审批中
                {
                    if (strstatus == "0")//待审批
                    {
                        cancz = true;
                    }
                }


                if (strope == "3")//已作废
                {
                    if (strstatus == "2")//已通过
                    {
                        cancz = true;
                    }
                }

                //if (strope == "")//激活
                //{
                //    if (strstatus == "4")//已作废
                //    {
                //        cancz = true;
                //    }
                //}

                if (strope == "2")//审核通过
                {
                    if (strstatus == "4" || strstatus == "1" || strstatus == "0")//已作废 审核中 提交
                    {
                        cancz = true;
                    }
                }
            }

            return cancz;

            //return true;
        }

        public static async Task<bool> GetEditPower(string ywid, TableName name)
        {
            string strsql = "select status  from " + listTableName[name.ToString()] + " where id = '" + ywid + "'";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(strsql, null);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                string strstatus = table.Rows[i]["status"].ToString();
                //string strspl = table.Rows[i]["spl"].ToString();

                if (strstatus == "0" || strstatus == "1" || strstatus == "2")//|| strstatus == "5"
                {
                    return false;
                }
            }

            return true;
        }

        public static async Task<bool> GetEditPowerSJ(string ywid, TableName name)
        {
            string strsql = "select status,jszt  from " + listTableName[name.ToString()] + " where id = '" + ywid + "'";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(strsql, null);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                string strstatus = table.Rows[i]["status"].ToString();
                string strjszt = table.Rows[i]["jszt"].ToString();

                //if (strstatus == "2" || strstatus == "1" || strstatus == "4" || strstatus == "5")
                if (strjszt != "3")
                {
                    if (strstatus == "0" || strstatus == "1" || strstatus == "2")//|| strstatus == "5"
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        //招标这里比价特殊
        public static async Task<bool> GetEditPowerZB(string ywid, TableName name)
        {
            string strsql = "select status,lx  from " + listTableName[name.ToString()] + " where id = '" + ywid + "'";
            DataTable table = await new RepositoryFactory().BaseRepository().FindTable(strsql, null);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                string strlx = table.Rows[i]["lx"].ToString();

                string strstatus = table.Rows[i]["status"].ToString();
                //string strspl = table.Rows[i]["spl"].ToString();

                if (strlx == "zb")
                {
                    if (strstatus == "2" || strstatus == "1" || strstatus == "0")//|| strstatus == "4" || strstatus == "5"
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        //TableName name
        public static async Task<bool> SetStatus(string ywid, string name, string status, string uname, string userid, decimal sdje, string additionInfo)
        {
            try
            {
                SPLISTUSERCONT splog = new SPLISTUSERCONT();
                splog.id = CommonHelper.GetUniqueId().ToString();
                splog.cid = ywid;
                splog.scid = listTableName[name];
                splog.userid = userid;
                splog.ispost = status;
                splog.createdate = DateTime.Now;
                splog.createuser = uname;

                return await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    string strsql = "update " + listTableName[name] + " set status = '" + status + "' where id = '" + ywid + "'";
                    await irp.ExecuteSql(strsql);

                    //添加审批记录
                    await irp.Insert(splog);

                    //保证金
                    await GetSHBZJ2String(name, ywid, irp, status);

                    //变更合同信息
                    await GetSHBZJString(name, ywid, irp, status);

                    //预算变更审批
                    await GetSHYSBGJEString(name, ywid, irp, status);

                    //付款申请
                    await GetSHfksqJEString(name, ywid, irp, status, additionInfo);

                    //变更审定金额
                    if (sdje > 0)
                    {
                        strsql = "update " + listTableName[name] + " set " + listSHJE[name] + "='" + sdje + "' where id = '" + ywid + "'";
                        await irp.ExecuteSql(strsql);
                    }

                    //下面是保存审核过程中的填报的字段
                    string strsqlext = GetSHString(additionInfo, name, ywid);
                    if (!string.IsNullOrEmpty(strsqlext))
                    {
                        await irp.ExecuteSql(strsqlext);
                    }

                    return true;

                });


                //await new RepositoryFactory().BaseRepository().ExecuteSql(strsql);
                //return true;
            }
            catch (Exception)
            {
                return false;
            }
        }



        public static async Task<bool> GetSHfksqJEString(string name, string ywid, IRepository irp, string status, string additionInfo)
        {
            string strsql = "";
            try
            {
                if (TableName.付款申请.ToString() == name && status == "2")
                {
                    if (!string.IsNullOrEmpty(additionInfo))
                    {
                        CProFKSQ yse = await irp.FindEntity<CProFKSQ>(new { id = ywid });

                        var addi = additionInfo.ToObject<dynamic>();
                        string czshje = addi.czshje;

                        yse.czshje = CommonHelper.ParseDecimal(czshje);

                        //更新预算变更审批
                        await irp.Update(yse);
                    }
                }

                return true;
            }
            catch
            {
                return false;
            }
        }


        //项目年预算审批
        public static async Task<bool> GetSHYSBGJEString(string name, string ywid, IRepository irp, string status)
        {
            string strsql = "";
            try
            {
                if (TableName.项目预算变更.ToString() == name && status == "2")
                {

                    CProManCZBKbg ysbg = await irp.FindEntity<CProManCZBKbg>(new { id = ywid });

                    CProManCZBK yse = await irp.FindEntity<CProManCZBK>(new { id = ysbg?.ysid });

                    yse.je = ysbg?.je;

                    //更新预算变更审批
                    await irp.Update(yse);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        //保证金
        public static async Task<bool> GetSHBZJ2String(string name, string ywid, IRepository irp, string status)
        {
            string strsql = "";
            try
            {
                if (TableName.合同保证金.ToString() == name && status == "2")
                {
                    CProConBZJ conbzj = await irp.FindEntity<CProConBZJ>(new { id = ywid });
                    conbzj.bzjzt = "已退还";
                    conbzj.thje = conbzj.bzjje;

                    //更新合同
                    await irp.Update(conbzj);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        //变更申请
        public static async Task<bool> GetSHBZJString(string name, string ywid, IRepository irp, string status)
        {
            string strsql = "";
            try
            {
                if (TableName.合同修改记录.ToString() == name && status == "2")
                {

                    CProContractbglog cProContract = await irp.FindEntity<CProContractbglog>(new { id = ywid });

                    CProContract contract = await irp.FindEntity<CProContract>(new { id = cProContract?.htid });

                    contract.jf = cProContract?.jf;
                    contract.yf = cProContract?.yf;
                    contract.htrq = cProContract?.htrq;
                    contract.htxz = cProContract?.htxz;
                    contract.htlx = cProContract?.htlx;
                    contract.htje = cProContract?.htje;
                    contract.kgrq = cProContract?.kgrq;
                    contract.jgrq = cProContract?.jgrq;
                    contract.xmjl = cProContract?.xmjl;
                    contract.jkfs = cProContract?.jkfs;
                    contract.cfd = cProContract?.cfd;
                    contract.qddd = cProContract?.qddd;
                    contract.fkzytj = cProContract?.fkzytj;
                    contract.wyzr = cProContract?.wyzr;
                    contract.qtsx = cProContract?.qtsx;
                    contract.remark = cProContract?.remark;
                    contract.remark2 = cProContract?.remark2;
                    contract.remark3 = cProContract?.remark3;
                    contract.htmc = cProContract?.htmc;
                    contract.yfk = cProContract?.yfk;
                    contract.zzqxq = cProContract?.zzqxq;
                    //contract.htxmmc = cProContract?.htxmmc;
                    //contract.htdxmc = cProContract?.htdxmc;
                    //contract.gsid = cProContract?.gsid;
                    //contract.ccode = cProContract?.ccode;
                    //contract.depname = cProContract?.depname;

                    //更新合同
                    await irp.Update(contract);

                    //删除三方
                    await irp.DeleteAny<CProContractSF>(new { htid = contract.id });

                    string strsqlsf = "select * from CProContractSF where htid= '" + ywid + "'";
                    var htsf = (await irp.FindList<CProContractSF>(strsqlsf));
                    foreach (var item in htsf)
                    {
                        item.id = CommonHelper.GetUniqueId().ToString();
                        item.htid = contract.id;

                        await irp.Insert(item);
                    }

                    //银行
                    await irp.DeleteAny<BConstructionUnitsYH>(new { htid = contract.id });

                    strsqlsf = "select * from BConstructionUnitsYH where htid= '" + ywid + "'";
                    var htyh = (await irp.FindList<BConstructionUnitsYH>(strsqlsf));
                    foreach (var item in htyh)
                    {
                        item.id = CommonHelper.GetUniqueId().ToString();
                        item.htid = contract.id;

                        await irp.Insert(item);
                    }

                    //付款计划
                    await irp.DeleteAny<CProConFKJH>(new { htid = contract.id });

                    strsqlsf = "select * from CProConFKJH where htid= '" + ywid + "'";
                    var htfkjh = (await irp.FindList<CProConFKJH>(strsqlsf));
                    foreach (var item in htfkjh)
                    {
                        item.id = CommonHelper.GetUniqueId().ToString();
                        item.htid = contract.id;

                        await irp.Insert(item);
                    }

                    //负责人
                    await irp.DeleteAny<CProConFuZeRen>(new { htid = contract.id });

                    strsqlsf = "select * from CProConFuZeRen where htid= '" + ywid + "'";
                    var htfzr = (await irp.FindList<CProConFuZeRen>(strsqlsf));
                    foreach (var item in htfzr)
                    {
                        item.id = CommonHelper.GetUniqueId().ToString();
                        item.htid = contract.id;

                        await irp.Insert(item);
                    }

                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        public static string GetSHString(string additionInfo, string name, string ywid)
        {
            string strsql = "";
            try
            {
                string sjdw = "";
                string sjdwrq = "";
                if (!string.IsNullOrEmpty(additionInfo))
                {
                    var addi = additionInfo.ToObject<dynamic>();
                    if (TableName.结算.ToString() == name)
                    {
                        sjdw = addi.sjdw;

                        sjdwrq = addi.sjdwrq;

                        string strdwrq = "";
                        if (!string.IsNullOrEmpty(sjdwrq))
                        {
                            strdwrq = " ,sjdwrq = '" + sjdwrq + "'";
                        }

                        if (!string.IsNullOrEmpty(sjdw))
                        {
                            strsql = "update CProJS set sjdw = '" + sjdw + "' " + strdwrq + " where id = '" + ywid + "'";
                        }
                    }
                }
            }
            catch
            {

            }



            return strsql;
        }

    }
}
